<?php
/**
 * ------------------------------------------------------------------------
 *   
 *   Sql class v1.0
 *   ---------
 *
 *  Made by Jacques Bodin-Hullin
 *     Jardin <jacques@bodin-hullin.net>
 *
 *   <http://www.bodin-hullin.net/?file=script#sql>
 *
 *  Started on September 14, 2007
 *  Last update September 14, 2007
 *
 * ------------------------------------------------------------------------
 *
 *  This script is to facilitate the interaction with your DBMS.
 *
 *  Copyright (C) 2007  Jacques 'Jardin' Bodin-Hullin
 *
 *   This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ------------------------------------------------------------------------
 */



//Singleton

class sql {

   //Vars
   public static $time = 0;
   public static $count = 0;

   /**
    * Class constructor
    */
   private function __construct() {}

   /*
    * List of functions:
    * connect
    * close
    * query
    * fetch_array
    * fetch_assoc
    * fetch_row
    * num_rows
    */

   /**
    * Connect to Mysql
    *
    * @param host string Hostname
    * @param user string Username
    * @param pass string Password
    * @param name string Name of the database (option)
    */
   public function connect($host, $user, $pass, $name = false) {
      $microtime[] = microtime();//time

      $db = mysql_connect($host, $user, $pass); //connect

      if ($name !== false) {//if the user want to select a db
         mysql_select_db($name, $db);
      }

      $microtime[] = microtime();//time
      self::$time += round(($microtime[1] - $microtime[0]), 2);//set time
   }

   /**
    * Select a database
    *
    * @param name string The database's name
    * @param db ... Result of sql connect
    */
   public function select_db($name, $db = false) {
      if (!empty($name)) {
         if ($db !== false) {
            $microtime[] = microtime();//time
            mysql_select_db($name, $db);//select db
            $microtime[] = microtime();//time
         }
         else {
            $microtime[] = microtime();//time
            mysql_select_db($name, $db);//select db
            $microtime[] = microtime();//time
         }

         self::$time += $microtime[1] - $microtime[0];//set time
      }

      return false;
   }

   /**
    * Disconnect to Mysql
    */
   public function close() {
      @mysql_close();
   }

   /**
    * Return the response of the sql query
    *
    * @pram sql string The query
    */
   public function query($sql) {
      if (!empty($sql)) {
         $microtime[] = microtime();//time

         $resp = mysql_query($sql) or die(mysql_error());

         $microtime[] = microtime();//time
         self::$time += $microtime[1] - $microtime[0];//set time

         self::$count++;//add 1 to the queries count

         return $resp;
      }

      return false;
   }

   /**
    * Make a fetch_array
    *
    * @param resp array The response of the sql query
    */
   public function fetch_array($resp) {
      if (!empty($resp)) {
         $e = mysql_fetch_array($resp);
         return $e;
      }

      return false;
   }

   /**
    * Make a fetch_assoc
    *
    * @param resp array The response of the sql query
    */
   public function fetch_assoc($resp) {
      if (!empty($resp)) {
         $e = mysql_fetch_assoc($resp);
         return $e;
      }

      return false;
   }

   /**
    * Make a fetch_row
    *
    * @param resp array The response of the sql query
    */
   public function fetch_row($resp) {
      if (!empty($resp)) {
         $e = mysql_fetch_row($resp);
         return $e;
      }

      return false;
   }

   /**
    * Count the query's response array
    *
    * @param resp array The SQL's response
    */
   public function num_rows($resp) {
      $buff = mysql_num_rows($resp);
      return $buff;
   }

   /**
    * Escape sql query
    *
    * @param text string String to escape
    */
   public function escape($text) {
      $buff = mysql_real_escape_string($text);
      return $buff;
   }


}
/** END **/


?>
